package com.example.qimo.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

//数据库操作类
public class MyDiaryDatabase {
    //定义创建数据库表的基本信息
    public static final String Database_name = "zsrj_db";//数据库名称
    public static final int Database_Version = 1;//数据库的版本号
    public static final String Table_Name = "diary";//表名，日记
    public static final String ID = "nid";//编号主键
    public static final String Title = "title";//标题
    public static final String Body = "body";//正文
    public  Context context;//上下文
    public  SQLiteDatabase db;//数据库对象
    public  DatabaseHelper mOpenHelper;//数据库操作的辅助类,版本管理

    public MyDiaryDatabase(Context context) {
        this.context = context;
        mOpenHelper = new DatabaseHelper(context);
    }

    private class DatabaseHelper extends SQLiteOpenHelper {

        public DatabaseHelper(@Nullable Context context) {
            super(context, Database_name, null, Database_Version);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {

        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        }
    }

    //创建数据库
    public boolean openDb() {
        int mode = Context.MODE_PRIVATE;
        try {
            db = context.openOrCreateDatabase(Database_name, mode, null);
            return true;
        } catch (SQLException e) {
            return false;
        }
    }

    //创建数据表
    public boolean onCreateTable() {
        String table_create = " create table if not Exists " + Table_Name + " ("
                + ID + " INTEGER primary key autoincrement,"
                + Title + " text not null,"
                + Body + " text not null);";
        //打开或者创建数据库
        try {
//            db.execSQL("drop table if exists " + Table_Name);//创建表之前先清空
            db.execSQL(table_create);//创建数据库表
            return true;
        } catch (SQLException e) {
            return false;
        }
    }

    //插入数据
    public boolean insertData(ContentValues values) {
        try {
            db.insert(Table_Name, ID, values);
            return true;
        } catch (SQLException e) {
            return false;
        }
    }

    //删除数据
    public boolean deleteData(Integer id) {
        try {
            String args[] = {id.toString()};
            db.delete(Table_Name, ID + "=?", args);
            return true;
        } catch (SQLException e) {
            return false;
        }
    }

    //修改数据
    public boolean upData(ContentValues values, Integer id) {
        try {
            String args[] = {id.toString()};
            db.update(Table_Name, values, ID + "=?", args);
            return true;
        } catch (SQLException e) {
            return false;
        }
    }

    //查询数据
    public Cursor getData(Integer id) {
        String args[] = {id.toString()};
        String col[] = {Title, Body};
        Cursor cur = db.query(Table_Name, col, ID + "=?", args, null, null, null);
        return cur;
    }

    public Cursor GetAllData() {
        String col[] = {ID, Title, Body};
        return db.query(Table_Name, col, null, null, null, null, null);
    }
}
